﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerTaiSanDao : ITaiSanDao
    {
        public SqlServerTaiSanDao()
        {
            SQLUtility.TableName = "TaiSan";
        }

        //Trả về đối tượng báo ăn
        private TaiSan MakeTaiSan(DataRow row)
        {
            long _id = long.Parse(row["TaisanID"].ToString());
            string _mataisan = row["Mataisan"].ToString();
            string _tenTaisan = row["TenTaisan"].ToString();
            int _schoolID = int.Parse(row["SchoolID"].ToString());
            return new TaiSan(_id, _mataisan, _tenTaisan, _schoolID);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<TaiSan> MakeTaiSans(DataTable dt)
        {
            IList<TaiSan> list = new List<TaiSan>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeTaiSan(row));
            }
            return list;
        }
        private DbParameter[] Take(TaiSan TaiSan)
        {
            DbParameter[] parameters = new DbParameter[4];
            parameters[0] = new SqlParameter("@TaisanID", TaiSan.Id);
            parameters[1] = new SqlParameter("@Mataisan", TaiSan.Mataisan);
            parameters[2] = new SqlParameter("@TenTaisan", TaiSan.TenTaisan);

            parameters[3] = new SqlParameter("@SchoolID", TaiSan.SchoolID);

            return parameters;
        }
        public IList<Business_Objects.TaiSan> GetTaiSan()
        {
            SQLUtility.TableName = "TaiSan";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeTaiSans(dt);
        }

        public Business_Objects.TaiSan GetTaiSanByID(string TaiSanID)
        {
            SQLUtility.TableName = "TaiSan";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@TaisanID", TaiSanID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeTaiSan(row);
            }
        }

        public void InsertTaiSan(Business_Objects.TaiSan TaiSan)
        {
            SQLUtility.TableName = "TaiSan";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(TaiSan), true);
        }

        public int UpdateTaiSan(Business_Objects.TaiSan TaiSan)
        {
            SQLUtility.TableName = "TaiSan";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(TaiSan)); }
            catch { return -1; }
        }

        public int DeleteTaiSan(Business_Objects.TaiSan TaiSan)
        {
            SQLUtility.TableName = "TaiSan";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(TaiSan)); }
            catch { return -1; }
        }


        public List<TaiSan> GetTaiSanBySchoolId(int schoolId)
        {
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectTaiSansByAndSchoolID", parameters);
            return MakeTaiSans(dt).ToList(); 
        }
    }
}
